﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using Framework.Library.Base.Base;
using Framework.Library.Base.Extensions;
using Framework.Library.Core.Utils;
using Framework.Library.Data.DAL;
using Framework.Library.Data.DAL.JObject;
using Framework.Library.Data.DAO;
using ServiceStack.Text;
using Framework.Library.Core.Config;
using Framework.Library.Core.Utils.GlobalHelper;
using Framework.Library.Core.Utils.BBCodeParse;
using Framework.Library.Core.Utils.Media;
using Framework.Library.Core.Utils.Images;

namespace Framework.Portal.Layouts.Client.Components.Teenio
{
    public partial class EventDetailPlayer : BaseControl
    {
        public string MENUNAME = string.Empty;

        public EventDetailPlayer()
        {
        }

        public EventDetailPlayer(JSection section)
        {
            MENUNAME = section.title;
        }

        protected int articleId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                int.TryParse(Request.QueryString["a"], out articleId);
                if (articleId > 0)
                {
                    TeenioContent info = ManagerFactory.TeenioContentManager.GetInfo(articleId);
                    if (info != null)
                    {
                        int menuId;
                        int.TryParse(Request.QueryString["m"], out menuId);
                        if (menuId == info.MENUID)
                        {
                            if (info.CONTENT_TYPE == (int)TeenioContentType.Audio)
                                BindData(info);
                            else
                                Response.Redirect(cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, (int)PortalLayout.Article, info.CONTENT_ID));
                        }

                        else
                            Response.Redirect(cfg.ArticlePath(info.CONTENT_PORTALID, info.MENUID, info.CONTENT_ID));
                    }
                    else
                        Response.Redirect(Configurations.ROOTDIR);
                }
            }
        }

        private void BindData(TeenioContent info)
        {

            if (!string.IsNullOrEmpty(info.CONTENT_AVATAR))
                imgAvatar.ImageUrl = ImagesUtils.GetImage(info.CONTENT_AVATAR, 110, 90);
            lblCreateDate.Text = DateTimeHelper.formatDateTime(info.CONTENT_MODIFYDDATE);
            hplAuthor.NavigateUrl = "http://my.go.vn/?id=" + info.CONTENT_CREATEUSERID;
            hplAuthor.Text = info.CONTENT_CREATEUSERNAME;
            lblTitle.Text = info.CONTENT_HEADLINE;
            lblView.Text = ManagerFactory.DAOEntity.GetList<TeenioContentHistory>(new TeenioContentHistory() { AUDIOID = info.CONTENT_ID }).Count.ToString();
            lblContent.Text = BBCodeUtils.GetParser(info.CONTENT_BODY, ErrorMode.Strict);
            lblPlayer.Text = MediaUtils.MediaContent("MainPlayer", string.Empty, ImagesUtils.GetImage(info.CONTENT_PREAVATAR, 400, 280), info.CONTENT_HEADLINE, string.Empty);
        }
    }
}